// pages/my/relation/relation.js
import { 
  FANS_LIST,
  FOLLOWS_LIST,
  BLACKLIST
 } from "../../../request/api";

 var app
 var userId
 var pageToken = ""
Page({
  /**
   * 页面的初始数据
   */
  data: {
    dataList:[],
    pageSize: 20,
    totalPage: 1,
    start: 1,
    loading: false,
    type:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app = getApp();
    userId = app.globalData.userId;
    var type = options.type || '1';
    this.setData({
      type: type,
    })
    wx.setNavigationBarTitle({
      title: type == 1? '关注': type == 2? '粉丝' : '我的黑名单',
    });
    this.loadMore();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    pageToken = ""
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // this.refresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadMore();
  },

  refresh: function(){
    wx.showNavigationBarLoading() //在标题栏中显示加载
    pageToken = '';
    this.data.dataList = [];
    this.loadMore();
  },
  loadMore: function(){
    switch(this.data.type){
      case "1":
        this.loadFollows({'id': userId, 'pageToken':pageToken})
        break;
      case "2":
        this.loadFans({'id': userId, 'pageToken':pageToken})
        break;
      case "3":
        this.loadBlacklist({'id': userId, 'pageToken':pageToken})
        break;
    }
  },

  loadFans: function(data){
    FANS_LIST(data).then(res =>{
      this.setListData(res)
    })
  },
  loadFollows: function(data){
    FOLLOWS_LIST(data).then(res =>{
      this.setListData(res)
    })
  },
  loadBlacklist: function(data){
    BLACKLIST(data).then(res =>{
      this.setListData(res)
    })
  },

  setListData: function(res){
    if(res.data.code == 1){
      var tempList = this.data.dataList
      if(pageToken.length == 0){
        tempList = res.data.result.items
      } else {
        tempList = tempList.concat(res.data.result.items)
      }
      pageToken = res.data.result.nextPageToken

      this.setData({
        dataList: tempList
      })
    }
    this.setData({
      loading: false
    })
    wx.hideNavigationBarLoading();
    // 数据成功后，停止下拉刷新
    wx.stopPullDownRefresh();
  },
})